package javafx.table.demo;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.stage.Stage;

/**
 * @author hanxi
 * 
 */
public class TableDemo extends Application {

	TableCreater creater = new TableCreater();
	BorderPane root = new BorderPane();
	Button normalButton = new Button("根据对象属性创建table");
	Button arrayButton = new Button("根据数组创建table");
	Button withCheckBoxButton = new Button("创建带多选按钮table");
	Button withRadioButton = new Button("创建带单选按钮table");
	Button withGraphicButton = new Button("创建带图表table");
	EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {
		@Override
		public void handle(MouseEvent event) {
			Object obj = event.getSource();
			if (obj == normalButton) {
				root.setCenter(creater.createTableByObject());
			} else if (obj == arrayButton) {
				root.setCenter(creater.createTableByStringArray());
			} else if (obj == withCheckBoxButton) {
				root.setCenter(creater.createTableWithCheckBox());
			} else if (obj == withRadioButton) {
				root.setCenter(creater.createTableWithRadioButton());
			} else if (obj == withGraphicButton) {
				root.setCenter(creater.createTableWithGraphic());
			}
		}
	};

	@Override
	public void start(Stage primaryStage) throws Exception {
		root.setCenter(creater.createTableWithGraphic());
		normalButton.addEventFilter(MouseEvent.MOUSE_CLICKED, handler);
		arrayButton.addEventFilter(MouseEvent.MOUSE_CLICKED, handler);
		withCheckBoxButton.addEventFilter(MouseEvent.MOUSE_CLICKED, handler);
		withRadioButton.addEventFilter(MouseEvent.MOUSE_CLICKED, handler);
		withGraphicButton.addEventFilter(MouseEvent.MOUSE_CLICKED, handler);
		HBox hbox = HBoxBuilder
				.create()
				.alignment(Pos.CENTER)
				.padding(new Insets(5))
				.children(normalButton, arrayButton, withCheckBoxButton,
						withRadioButton, withGraphicButton).build();
		root.setTop(hbox);
		root.setCenter(creater.createTableByObject());
		Scene scene = new Scene(root, 700, 500);
		scene.getStylesheets().add(
				getClass().getResource("table-chart.css").toExternalForm());
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
